package com.module.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.module.framework.constants.SystemConstants;
import com.module.framework.domain.Menu;
import com.module.framework.domain.Role;
import com.module.framework.mapper.MenuMapper;
import com.module.framework.mapper.RoleMapper;
import com.module.framework.service.MenuService;
import com.module.framework.service.RoleService;
import com.module.framework.vo.MenuTreeVO;
import com.module.framework.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author JiHuiQin
 * @Date 2022/3/2 19:52
 * @Version 1.0
 */

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private MenuService menuService;

	@Override
	public PageVO getRoleList(Integer pageNum, Integer pageSize, String name) {
		LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(Objects.nonNull(name), Role::getName, name);
		PageVO pageVO = null;
		if (pageNum == null && pageSize == null) {
			List<Role> lists = list(queryWrapper);
			pageVO = new PageVO(lists, lists.size());
		} else {
			pageSize = pageSize == null ? SystemConstants.PAGE_SIZE : pageSize;
			Page<Role> page = new Page<>(pageNum, pageSize);
			page(page, queryWrapper);
			List<Role> records = page.getRecords();
			pageVO = new PageVO(records, (int) page.getTotal());
		}
		return pageVO;
	}

	@Override
	public void addRole(Role role) {
		roleMapper.insert(role);
	}

	@Override
	public void updateRole(Role role) {
		roleMapper.updateById(role);
	}

	@Override
	public void deleteRole(Integer id) {
		roleMapper.deleteById(id);
	}

}
